package edu.xhu.communityantiepidemicsystemback.common.interceptor;

import edu.xhu.communityantiepidemicsystemback.common.utils.Md5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        String auth = request.getHeader("x-auth-code");
        if (StringUtils.isEmpty(auth)) {
            response.sendRedirect("/api/auth/401");
            return false;
        }
        if (auth.equals(Md5Utils.md5())) {
            return true;
        } else {
            response.sendRedirect("/api/auth/401");
            return false;
        }
    }


}
